package com.huawei.org.controller;

<<<<<<< HEAD
import com.huawei.basic.anno.PreAuthorize;
=======
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf
import com.huawei.basic.utils.AjaxResult;
import com.huawei.basic.utils.PageList;
import com.huawei.org.domain.Department;
import com.huawei.org.query.DepartmentQuery;
import com.huawei.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* @Description: 部门controller类
* @Author: huawei
* @Version: 1.0
* @Date:  2022/4/22 9:02
*/
@RestController // 如果你在类上使用了此注解，那么接口就只能返回JSON数据
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: findDepartmentById
    * @Description: 使用Restful风格编写根据Id查询接口
    * @Author: huawei
    * @Version: 1.0
    * @Date:  2022/4/22 9:09
    * @Parameters: * @param id
    * @Return Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findDepartmentById(@ApiParam(name="部门主键ID属性",required = true) @PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
    * @Title: findDepartments
    * @Description: 高级查询，查询所有
    * @Author: huawei
    * @Version: 1.0
    * @Date:  2022/4/22 9:26
    * @Parameters: * @param departmentQuery
    * @Return java.util.List<Department>
    */
    @PostMapping // http://localhost:8080/dept  post
    public PageList findDepartments(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
    * @Title: delDepartmentById
    * @Description: 根据Id删除
    * @Author: huawei
    * @Version: 1.0
    * @Date:  2022/4/22 9:40
    * @Parameters: * @param id
    * @Return AjaxResult
    */
    @DeleteMapping("/{id}")
<<<<<<< HEAD
    @PreAuthorize(name="删除一条记录",value="department:deleteById")
=======
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf
    public AjaxResult delDepartmentById(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
    * @Title: addOrUpdate
    * @Description: 新增或者修改
    * @Author: huawei
    * @Version: 1.0
    * @Date:  2022/4/22 9:45
    * @Parameters: * @param id
    * @Return AjaxResult
    */
    @PutMapping
<<<<<<< HEAD
    @PreAuthorize(name="新增或修改",value="department:addOrUpdate")
=======
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
    * @Title: batchDelDept
    * @Description: 批量删除
    * @Author: huawei
    * @Version: 1.0
    * @Date:  2022/4/23 9:49
    * @Parameters: * @param id
    * @Return AjaxResult
     * 前端传递数组的时候：
     *  后端使用集合或者是数组接收
    */
    @PatchMapping
    public AjaxResult batchDelDept(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
    * @Title: tree
    * @Description: 查询部门树数据
    * @Author: huawei
    * @Version: 1.0
    * @Date:  2022/4/23 15:46
    * @Parameters: * @param
    * @Return java.util.List<Department>
    */
    @GetMapping("/tree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }

}
